<?php
/**
 * User: xuanbang.wen
 * Date: 2019/12/12
 */

namespace App\Web\Controller;


use App\Service\Dict;
use App\Service\DictConstant;
use App\Service\ExternalAPI\EhireApi;
use EchoPHP\Facades\Db;
use EchoPHP\Http\Request;
use App\Web\Model\CaseContacter as CaseContacterModel;
use App\Web\Model\Project as ProjectModel;

class CaseContacter extends Controller {

    public function show(Request $request) {
        $projectId = $request->getParam('projectid');
        $result = (new ProjectModel())->chkGetRight($projectId);
        if (!$result) {
            return $this->json([
                'status' => false,
                'message' => '项目不存在或无权查看',
                'caselist' => []
            ]);
        }
        $allCase = (new CaseContacterModel())->show($projectId);
        $attributes = [
            'area' => [DictConstant::DD_CITY_COUNTY, '']
        ];
        $dict = new Dict();
        $dict->rowTrans($allCase, $attributes, true, ',', '/');
        return $this->json([
            'status' => true,
            'massage' => '查询成功',
            'caselist' => $allCase,
        ]);
    }

    public function detail(Request $request) {
        $caseId = $request->getParam('caseid');
        return $this->json([
            'status' => true,
            'massage' => '查询成功',
            'contacterlist' => (new CaseContacterModel())->detail($caseId),
        ]);
    }

    public function add(Request $request) {
        $caseId = $request->getParam('caseid');
        $projectId = $request->getParam('projectid');
        $projectModel = new ProjectModel();
        if (!$projectModel->chkEditRight($projectId)) {
            return $this->json(['status' => false, 'message' => '无修改权限']);
        }
        $account = [
            'accountid' => $request->getParam('accountid', 0),
            'email' => $request->getParam('email', ''),
            'cname' => $request->getParam('cname', ''),
            'ischarger' => $request->getParam('ischarger', 0),
            'bindehire' => $request->getParam('bindehire', 0),
            'ehirectmid' => $request->getParam('ehirectmid', ''),
            'ehireaccid' => $request->getParam('ehireaccid', ''),
        ];
        return $this->json((new CaseContacterModel())->add($account, $caseId));
    }

    public function del(Request $request) {
        $caseContacterId = $request->getParam('casecontacterid');
        $caseId = $request->getParam('caseid');
        $projectId = $request->getParam('projectid');
        $projectModel = new ProjectModel();
        if (!$projectModel->chkEditRight($projectId)) {
            return $this->json(['status' => false, 'message' => '无修改权限']);
        }
        return $this->json((new CaseContacterModel())->del($caseId, $caseContacterId));
    }

    /**
     * 一个职位批量设置职位联系人
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function batchAddContactor(Request $request) {
        $caseId = $request->getParam('caseid');
        $projectModel = new ProjectModel();
        $sql = 'select projectid from SCase where caseid = ? and status != ?';
        $var = [$caseId, '03'];
        $result = Db::selectOne($sql, $var);
        if (!empty($result)) {
            $projectId = $result['projectid'];
        } else {
            return $this->json([
                'status' => false,
                'message' => '该职位不存在或已结束'
            ]);
        }
        if (!$projectModel->chkEditRight($projectId)) {
            return $this->json(['status' => false, 'message' => '无修改权限']);
        }
        $contactorIds = $request->getPost('contactorids'); //多个字符串逗号隔开
        if (empty($contactorIds)) {
            return $this->json([
                'status' => false,
                'message' => '添加联系人不能为空'
            ]);
        }
        $contactorIdArr = explode(',', $contactorIds);
        $caseContactorModel = new CaseContacterModel();
        $result = $caseContactorModel->batchAddContactor($contactorIdArr, $projectId, $caseId);
        return $this->json($result);
    }
}